package com.upc.toss.slice;

import com.upc.toss.ResourceTable;
import com.upc.toss.SettingProvider;
import com.upc.toss.fraction.SecondFraction;
import com.upc.toss.fraction.MyFraction;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

public class  MainAbilitySlice extends AbilitySlice implements Component.ClickedListener{

    private Button mainBtn, secondBtn,myBtn;

    private Text equipmentName;
    private Text equipmentId;

    String eqId;
    String eqName;

    static public String username;

    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        username = intent.getStringParam("name");

        mainBtn= (Button) findComponentById(ResourceTable.Id_main_btn);
        secondBtn = (Button) findComponentById(ResourceTable.Id_second_btn);
        myBtn = (Button) findComponentById(ResourceTable.Id_my_btn);

        equipmentName = (Text) findComponentById(ResourceTable.Id_text_equipment_name_main);

        equipmentName.setText(eqName);

        //changeImage = (Image) findComponentById(ResourceTable.Id_text_change_image);
        mainBtn.setClickedListener(this);
        secondBtn.setClickedListener(this);
        myBtn.setClickedListener(this);
        //changeImage.callOnClick();
        addMainFraction();

    }
    public String getName(){
        return username;
    }

    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        super.onAbilityResult(requestCode, resultCode, resultData);
        if(requestCode==666){
            eqId = resultData.getStringParam("eqId");
            eqName = resultData.getStringParam("eqName");

        }
    }

    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        super.onResult(requestCode, resultIntent);
        if(requestCode==666){
            eqId = resultIntent.getStringParam("eqId");
            eqName = resultIntent.getStringParam("eqName");

            equipmentName.setText(eqName);
        }
    }

    private void  addMainFraction(){
        ((FractionAbility)getAbility()).getFractionManager()
                .startFractionScheduler()
                .add(ResourceTable.Id_mainstack, new com.upc.toss.fraction.MainFraction())
                .submit();
    }

    private void  replaceMainFraction(){
        ((FractionAbility)getAbility()).getFractionManager()
                .startFractionScheduler()
                .replace(ResourceTable.Id_mainstack, new com.upc.toss.fraction.MainFraction())
                .submit();
    }
    private void  replaceSecondFraction(){
        ((FractionAbility)getAbility()).getFractionManager()
                .startFractionScheduler()
                .replace(ResourceTable.Id_mainstack, new SecondFraction())
                .submit();
    }
    private void  replaceMyFraction(){
        ((FractionAbility)getAbility()).getFractionManager()
                .startFractionScheduler()
                .replace(ResourceTable.Id_mainstack, new MyFraction())
                .submit();

    }

    public void onActive() {
        super.onActive();
    }
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
    public void onBackground() {
        super.onBackground();
    }

    @Override
    public void onClick(Component component) {

        switch (component.getId()){
            case ResourceTable.Id_main_btn:
                replaceMainFraction();
                break;
            case ResourceTable.Id_second_btn:
                replaceSecondFraction();
                break;
            case ResourceTable.Id_my_btn:

                replaceMyFraction();
                break;
            default:
                break;
        }
    }

}
